package com.example.phoenixshop.config;

import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;

@Component
public class AliPayConfig implements ApplicationRunner {

    @Value("${alipay.appId}")  //应用Id
    private String appId;

    @Value("${alipay.privateKey}")    //私钥
    private String privateKey;

    @Value("${alipay.publicKey}")     //公钥
    private String publicKey;

    @Value("${alipay.gateway}")     //支付宝网关
    private String gateway;

    @Value("${alipay.notifyUrl}")    //支付成功后的接口回调地址，非回调的友好页面
    private String notifyUrl;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        Factory.setOptions(getOptions());
    }

    private Config getOptions() {
        //主要配置

        Config config = new Config();
        //设置请求的协议
        config.protocol = "https";
        //设置网关端口
        config.gatewayHost = this.gateway;
        //设置标记类型
        config.signType = "RSA2";

        config.appId = this.appId;

        // 为避免私钥随源码泄露，从文件中读取私钥字符串
        config.merchantPrivateKey = this.privateKey;

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = this.publicKey;

        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = notifyUrl;

        return config;
    }
}